$(function () {
    let markId = getUrlString('markid');
    let kutoysid = getUrlString('kutoysid');
    let payresult = getUrlString('payresult');
    let id = getUrlString('poolid');
    let itemmoney3;
    let itemmoney2;
    let maxusermoney3;
    let topHeight;
    let isBuyHp = true;
    let myMoney2 = '';
    let rmbMoney;
    let HPmoney3 = 0;
    //获取用户信息
    function getUserInfo() {
        getData('post','/niu/apiv3/member_userinfo.php',{'markid':markId},function (data) {
            console.log(data);
            myMoney2 = Number(data.data.money2);
            detail();
        })
    }
    getUserInfo();
    function detail() {
        let data = {
            'markid':markId,
            'poolid':id
        };
        getData('post','/niu/apiv4/niu_getdetail_hp_bukuan.php',data,function (data) {
            console.log(data);
            // $('.userface').attr('src',data.userface);
            // $('.toyspic').attr('src',data.toyspic);
            $('#price').html(data.ToyInfo.price);
            $('#mainseries').html(data.ToyInfo.mainseries);
            $('#maker').html(data.ToyInfo.maker);
            $('#secondseries').html(data.ToyInfo.secondseries);
            var time = data.ToyInfo.book_time.substring(0,4) + '年' + data.ToyInfo.book_time.substring(4,6)+'月';
            if (data.ToyInfo.book_time === '0'){
                $('.fssj').html('无');
            } else {
                $('.fssj').html(time);
            }
            if (data.ToyInfo.sptype === '2'){
                $('.spzt').html('预售商品');
            } else {
                $('.spzt').html('现货商品')
            }
            itemmoney3 = data.detailInfo.extra_money3;
            itemmoney2 = data.detailInfo.extra_money2;
            $('#HPPrice,#DJMoney3').html(data.detailInfo.extra_money3);
            $('#money2,#DJMoney2').html(data.detailInfo.extra_money2);
            let QKmoney2 = Number(data.detailInfo.money2) + Number(data.detailInfo.extra_money2);
            let QKHPPrice = Number(data.detailInfo.money3) + Number(data.detailInfo.extra_money3);
            $('#QKmoney2').html(QKmoney2);
            $('#QKHPPrice').html(QKHPPrice);

            $('#payhp').val(maxusermoney3);
            $('.toy_bigtitletext,.toysname,.indexName').html(data.detailInfo.name);
            isBuyHp = data.detailInfo.name.indexOf('特定蛋机道具') !== 0;
            $('.toyspic').attr('src',data.detailInfo.picdan);

            maxusermoney3 = Number(data.detailInfo.defaultPayMoney3);
            $('#defaultPayMoney3').html(maxusermoney3+'HP');
            let exmoney2 = Math.floor((itemmoney2 / itemmoney3 * maxusermoney3));
            $('#DKMoney2').html(exmoney2);
            let CEmoney2 = itemmoney2 - exmoney2;
            if ( myMoney2 - CEmoney2 < 0){
                rmbMoney = Math.ceil((itemmoney2 - myMoney2) / 6);
                $('#CEmoney2').html(myMoney2);
            }else {
                rmbMoney = 0;
                $('#CEmoney2').html(itemmoney2);
            }
            $('#rmbMoney').html(rmbMoney);
            //是否使用HP抵扣
            $('.HPDK .check').click(function () {
                $(this).hide();
                $('.HPDK .noCheck').show();

                HPmoney3 = 0;
                if ( myMoney2 - itemmoney2 < 0){
                    rmbMoney = Math.ceil((itemmoney2 - myMoney2) / 6);
                    $('#CEmoney2').html(myMoney2);
                }else {
                    rmbMoney = 0;
                    $('#CEmoney2').html(itemmoney2);
                }
                $('#rmbMoney').html(rmbMoney);
            });

            //是否使用HP抵扣
            $('.HPDK .noCheck').click(function () {
                $(this).hide();
                $('.HPDK .check').show();

                HPmoney3 = maxusermoney3;
                if ( myMoney2 - CEmoney2 < 0){
                    rmbMoney = Math.ceil((CEmoney2 - myMoney2) / 6);
                    $('#CEmoney2').html(myMoney2);
                }else {
                    rmbMoney = 0;
                    $('#CEmoney2').html(CEmoney2);
                }
                $('#rmbMoney').html(rmbMoney);
            });
            // if (data.detailInfo.is_taobao === 0){
            //     $('.goTaoBao').hide()
            // }else {
            //     $('.goTaoBao').show().click(function () {
            //         window.location.href = 'dancool://taobao='+data.detailInfo.picbg
            //     });
            // }
            // $('#payMoney2,#moreMoney2').html(data.detailInfo.defaultPayMoney2);

            $('.commodityPicture').html(escape2Html(data.detailInfo.intro));
            let append = '';
            $(data.ZanList).each(function () {
                append += ' <li>' +
                    '            <input type="hidden" value="'+this.id+'">'+
                    '            <img src="'+this.faceurl+'" alt=""/>' +
                    '        </li>'
            });
            $('#likeList').append(append);
            $('#likeCount').html(data.ZanCount);
            topHeight = $('.commodityPic').height() + 70;
        })
    }
    let isXy;
    //是否同意协议
    $('.tyxy .check').click(function () {
        $(this).hide();
        $('.tyxy .noCheck').show();
        isXy = 0
    });

    //是否同意协议
    $('.tyxy .noCheck').click(function () {
        $(this).hide();
        $('.tyxy .check').show();
        isXy = 1
    });

    $('#closeIndex').click(function () {
        nativeCommand('dancool://activity=fin')
    });
    $(window).scroll(function () {
        let top = $(window).scrollTop();//滚动条距离顶部的高度
        if (top > topHeight){
            $('.indexTitleName').fadeIn()
        } else {
            $('.indexTitleName').fadeOut()
        }
    });

    $('.buyBtn').click(function () {
        $('#moreMoney3').html($("#payhp").val());
        $('.alertIndex').show();
    });
    $('#closeAlert').click(function () {
        $('.alertIndex').hide()
    });
    console.log(payresult)
    if (payresult === 'true'){
        $('.alertIndex').show();
        layLoading();
        setTimeout(function () {
            layer.closeAll('loading');
            bugHP();
        },2000)
    }
    //确认购买
    $('#qrBtn').click(function () {
        if (isXy === 0){
            laymsg('请同意购买须知');
            return
        }
        if (rmbMoney > 0){
            nativeCommand('dancool://paymoneyfree='+rmbMoney)
        } else {
            bugHP();
        }

    });

    function bugHP (){
        let data = {
            'markid':markId,
            'poolid':id,
            'kutoysid':kutoysid,
            'times':1,
            'seed':0,
            'money3':HPmoney3,
        };
        console.log(data)
        getData('post','/niu/apiv4/niu_bukuan_hp.php',data,function (data) {
            console.log(data);
            if (data.resultcode === '0'){
                laymsg('购买成功');
                setTimeout(function () {
                    nativeCommand("dancool://activity=fin")
                },500)
            } else {
                laymsg(data.msg)
            }
        })
    }

    // function bugCup (){
    //     let money3 = $("#payhp").val();
    //     let data = {
    //         'markid':markId,
    //         'poolid':id,
    //         'money3':money3,
    //     };
    //     getData('post','/niu/apiv4/niu_bycoupon_hp.php',data,function (data) {
    //         console.log(data);
    //         if (data.resultcode === '0'){
    //             laymsg('购买成功');
    //             setTimeout(function () {
    //                 nativeCommand("dancool://activity=fin")
    //             },500)
    //         } else {
    //             laymsg(data.msg)
    //         }
    //     })
    // }



    // $(".payhp").bind("input propertychange", function () {
    //
    //     let payhpnumber = $("#payhp").val();
    //     if (payhpnumber === "") {
    //         payhpnumber = "0";
    //     }
    //
    //     if (payhpnumber < 0) {
    //         payhpnumber = 0;
    //         $(".payhp").val("0");
    //     } else if (payhpnumber > maxusermoney3) {
    //         payhpnumber = maxusermoney3;
    //         $(".payhp").val("" + maxusermoney3);
    //     } else {
    //         let inpayhp = parseInt(payhpnumber);
    //         $(".payhp").val("" + inpayhp);
    //     }
    //     let exmoney2 = Math.ceil((((itemmoney3 - payhpnumber) * itemmoney2) / itemmoney3));
    //     $('#moreMoney2').html(exmoney2);
    //     $(".payexgoldinfo").html("额外支付" + exmoney2 + "金币");
    //
    // });



});

function getnative(actionurl) {
    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (isiOS) {
        window.webkit.messageHandlers.getnative.postMessage(actionurl);
    }else {
        ikags.getnative(actionurl)
    }
}

// function keynumberPress() {
//     let keyCode = event.keyCode;
//     if ((keyCode >= 48 && keyCode <= 57)) {
//         event.returnValue = true;
//     } else {
//         event.returnValue = false;
//     }
// }
